﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Veralkohol
{
    /// <summary>
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Veralkohol.MyControls"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:Veralkohol.MyControls;assembly=Veralkohol.MyControls"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file.
    ///
    ///     <MyNamespace:AlcoholTooltip/>
    ///
    /// </summary>
    public class AlcoholTooltip : Control
    {
        AlcoholDiagramPoint point;
        bool lastPoint;
        bool isSmall;

        public event EventHandler TooltipClosed;
        public event EventHandler<DrinkDeletedEventArgs> AllDrinkDeleted;
        public event EventHandler<DrinkDeletedEventArgs> DrinkDeleted;

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            TextBlock drinktime = this.GetTemplateChild("drinktime") as TextBlock;
            if (drinktime != null)
            {

                drinktime.Text = string.Format("{0}. nap {1}:{2}     {3:0.##}‰",
                    (point.ComparableDrinkTime/60)/24,
                    (point.ComparableDrinkTime/60)%24,
                    point.ComparableDrinkTime%60,
                point.Thousandths);
            }

            TextBlock jozan_txt = this.GetTemplateChild("jozan_txt") as TextBlock;
            TextBlock jozan = this.GetTemplateChild("jozan") as TextBlock;
            if (jozan != null && jozan_txt != null)
            {
                if (isSmall || !lastPoint)
                {
                    jozan_txt.Visibility = System.Windows.Visibility.Hidden;
                    jozan.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    jozan_txt.Visibility = System.Windows.Visibility.Visible;
                    jozan.Visibility = System.Windows.Visibility.Visible;
                    double endtime = point.GetZero();
                    int dd = (int)(endtime / 24);
                    int hh = (int)(endtime - dd * 24);
                    int mm = (int)Math.Round((endtime - dd * 24 - hh) * 60, 0);
                    jozan.Text = string.Format("{0}.nap {1}:{2}", dd, hh, mm);
                }
            }

            TextBlock drinks_txt = this.GetTemplateChild("drinks_txt") as TextBlock;
            ListBox drinks = this.GetTemplateChild("drinks") as ListBox;
            Button delbutt = this.GetTemplateChild("delete") as Button;
            if (drinks_txt != null && drinks != null)
            {
                if (isSmall)
                {
                    drinks.Visibility = System.Windows.Visibility.Hidden;
                    drinks_txt.Visibility = System.Windows.Visibility.Hidden;
                    delbutt.Visibility = System.Windows.Visibility.Hidden;
                }
                else
                {
                    drinks.Visibility = System.Windows.Visibility.Visible;
                    drinks_txt.Visibility = System.Windows.Visibility.Visible;
                    delbutt.Visibility = System.Windows.Visibility.Visible;
                    drinks.ItemsSource = point.Drinks;
                }

                Button delDrink = this.GetTemplateChild("delDrink") as Button;
                if (delDrink != null)
                {
                    delDrink.Click += (s, e) =>
                    {
                        if (DrinkDeleted != null)
                            DrinkDeleted(drinks.SelectedItem, new DrinkDeletedEventArgs()
                            {
                                SelectedPoint = this.point
                            });
                    };

                    drinks.SelectionChanged += (s, e) =>
                    {
                        delDrink.Visibility = System.Windows.Visibility.Visible;
                    };
                }
            }

            Button close = this.GetTemplateChild("close") as Button;
            if (close != null)
            {
                close.Click += (s, e) =>
                    {
                        if (TooltipClosed != null)
                            TooltipClosed(this, null);
                    };
            }

            Button delfull = this.GetTemplateChild("delete") as Button;
            if (delfull != null)
            {
                delfull.Click += (s, e) =>
                {
                    if (AllDrinkDeleted != null)
                        AllDrinkDeleted(this, new DrinkDeletedEventArgs()
                        {
                            SelectedPoint = this.point
                        });
                };
            }

            
        }

        public AlcoholTooltip(AlcoholDiagramPoint p, bool last, bool little)
        {
            point = p;
            lastPoint = last;
            isSmall = little;
        }

        static AlcoholTooltip()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AlcoholTooltip), new FrameworkPropertyMetadata(typeof(AlcoholTooltip)));
        }
    }

    public class DrinkDeletedEventArgs : EventArgs
    {
        public AlcoholDiagramPoint SelectedPoint { get; set; }
    }
}
